package net.yebaihe.wc;

import java.util.HashMap;

import net.yebaihe.wc.DataMan.MSGTYPE;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class Login extends StateMachine {
	EditText edtEmail;
	EditText edtPasswd;
	

	@Override
	protected int getLayoutRes() {
		return R.layout.login;
	}

	@Override
	protected void prepareClient(View client) {
		edtEmail=(EditText) client.findViewById(R.id.edtEmail);
		edtPasswd=(EditText) client.findViewById(R.id.edtPasswd);
		Button btnLogin=(Button) client.findViewById(R.id.btnlogin);
		btnLogin.setOnClickListener(new OnClickListener(){
			public void onClick(View arg0) {
				edtEmail.setEnabled(false);
				edtPasswd.setEnabled(false);
				DataMan.get(Login.this).login(edtEmail.getText().toString(),edtPasswd.getText().toString());
			}
		});
		
		Button btnRegister=(Button) client.findViewById(R.id.btnRegister);
		btnRegister.setOnClickListener(new OnClickListener(){
			public void onClick(View arg0) {
				changeToActivity(Register.class);
			}
		});
		
	}

	@Override
	protected String getTopbarTitle() {
		return "登录";
	}

	@Override
	public void HandleMessage(MSGTYPE msgtype, boolean ret,
			HashMap<String, String> params) {
		super.HandleMessage(msgtype, ret, params);
		switch (msgtype){
		case LOGIN:
			if (ret){
				Toast.makeText(this, "登录成功！", Toast.LENGTH_LONG).show();
				getWCAppContext().email=edtEmail.getText().toString();
				Pref.get(this).save("email",getWCAppContext().email);
				getWCAppContext().updateMyLocation();
				//goback();
				finish();
				//changeToActivity(Index.class);
				//finish();
			}
			else{
				Toast.makeText(this, "登录失败！", Toast.LENGTH_LONG).show();
				edtEmail.setEnabled(true);
				edtPasswd.setEnabled(true);
			}
			break;
		}
	}

}
